package com.alkaid.manga.service.impl;

import com.alkaid.commons.entity.AnimePageEntity;
import com.alkaid.commons.entity.Manga;
import com.alkaid.commons.entity.MangaDetails;
import com.alkaid.commons.ex.DataNotFoundException;
import com.alkaid.manga.mapper.MangaDetailsMapper;
import com.alkaid.manga.mapper.MangaMapper;
import com.alkaid.manga.service.MangaService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.alkaid.commons.msg.DataTypeEnum.MANGA;
import static com.alkaid.commons.msg.DataTypeEnum.MANGA_DETAIL;

/**
 * @author Kylin
 */
@Service
public class MangaServiceImpl implements MangaService {

    private final MangaMapper mangaMapper;

    private final MangaDetailsMapper mangaDetailsMapper;

    public MangaServiceImpl(MangaMapper mangaMapper, MangaDetailsMapper mangaDetailsMapper) {
        this.mangaMapper = mangaMapper;
        this.mangaDetailsMapper = mangaDetailsMapper;
    }

    @Override
    public List<Manga> getHotList(Integer num) throws DataNotFoundException {
        LambdaQueryWrapper<Manga> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Manga::getId, Manga::getName, Manga::getHot).orderByDesc(Manga::getHot)
                .last("limit " + num);
        List<Manga> list = mangaMapper.selectList(wrapper);
        if (list == null || list.size() == 0) {
            throw new DataNotFoundException();
        }
        return list;
    }

    @Override
    public List<Manga> getCountList(Integer num) throws DataNotFoundException {
        LambdaQueryWrapper<Manga> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Manga::getId, Manga::getName, Manga::getCover, Manga::getCount, Manga::getTag, Manga::getAllNum,
                Manga::getRefreshNum, Manga::getStatus).orderByDesc(Manga::getCount).last("limit " + num);
        List<Manga> list = mangaMapper.selectList(wrapper);
        if (list == null || list.size() == 0) {
            throw new DataNotFoundException();
        }
        return list;
    }

    @Override
    public PageInfo<Manga> getAll(AnimePageEntity page) throws DataNotFoundException {
        PageHelper.startPage(page.getPage(), page.getRows(), page.getOrderBy());
        Page<Manga> list = mangaMapper.findAll(page.getSql());
        return new PageInfo<>(list);
    }

    @Override
    public Manga getMangaWithDetailsPlayByMangaId(Integer id) throws DataNotFoundException {
        Manga manga = mangaMapper.selectById(id);
        if (manga == null) {
            throw new DataNotFoundException(MANGA);
        }
        manga.setAuthor(mangaDetailsMapper.findAuthors(id));
        List<MangaDetails> mangaDetails = mangaDetailsMapper.findByMangaId(id);
        if (mangaDetails.size() == 0) {
            throw new DataNotFoundException(MANGA_DETAIL);
        }
        manga.setMangaDetails(mangaDetails);
        return manga;
    }
}
